﻿using MediatR;
using SheepCultivation.API.AddliCation.EnterPriseW;
using SheepCultivation.Common.Domain;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.DepartmentW
{
    public class DepartmentSeachHandler : IRequestHandler<DepartmentSeachCommon, Result>
    {
        public readonly IBaseRepository<Department> Departmentrepository;
        public readonly IBaseRepository<DepartmentType> DepartmentTypeRepository;

        public DepartmentSeachHandler(IBaseRepository<Department> departmentrepository, IBaseRepository<DepartmentType> departmentTypeRepository)
        {
            Departmentrepository = departmentrepository;
            DepartmentTypeRepository = departmentTypeRepository;
        }

        public async Task<Result> Handle(DepartmentSeachCommon request, CancellationToken cancellationToken)
        {
            var res = new Result() { Code = (int)CommonEnumCode.error };
            try
            {
                var data = new PageResult() { };
                var list = (from a in Departmentrepository.GetAll()
                            join b in DepartmentTypeRepository.GetAll()
                            on a.DepartmentTypeId equals b.DepartmentTypeId
                            select new DepartmentDto()
                            {
                                DepartmentId = a.DepartmentTypeId,
                                DepartmentNext = a.DepartmentNext,
                                DepartmentName = a.DepartmentName,
                                DepartmentIsNot = a.DepartmentIsNot,
                                DepartmentTypeId = a.DepartmentTypeId,
                                DepartmentTypeName = b.DepartmentTypeName,
                                DepartmentNum = a.DepartmentNum,
                                DepartmentAdd = a.DepartmentAdd,
                                DepartmentFullName = a.DepartmentNext + a.DepartmentName
                            }).ToList();
                data.PageCount = list.Count();
                data.TotalCount = (int)Math.Ceiling((list.Count() * 1.0) / request.pagesize);
                data.data = list.OrderBy(x => x.DepartmentTypeId).Skip((request.pageindex - 1) * request.pagesize).Take(request.pagesize).ToList();
                res.Code = (int)CommonEnumCode.success;
                res.Data = data;
                res.Msg = "部门列表展示";
            }
            catch (Exception)
            {

                throw;
            }

            return res;
        }
    }
    public class PageResult
    {
        public int PageCount { get; set; }
        public int TotalCount { get; set; }
        public List<DepartmentDto> data { get; set; }
    }
    public class DepartmentDto
    {
        public int DepartmentId { get; set; }
        /// <summary>
        /// 上级名称
        /// </summary>
        public string DepartmentNext { get; set; }
        /// <summary>
        /// 部门名称
        /// </summary>
        public string DepartmentName { get; set; }
        /// <summary>
        /// 部门停用状态（1启用 2 停用）
        /// </summary>
        public int DepartmentIsNot { get; set; }
        /// <summary>
        /// 部门类型（1行政管理，2生产加工，3业务销售）
        /// </summary>
        public int DepartmentTypeId { get; set; }
        /// <summary>
        /// 部门序号
        /// </summary>
        public int DepartmentNum { get; set; }
        /// <summary>
        /// 部门区域
        /// </summary>
        public string DepartmentAdd { get; set; }
        public string DepartmentTypeName { get; set; }
        /// <summary>
        /// 部门全称
        /// </summary>
        public string DepartmentFullName { get; set; }
    }
}
